home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
tagsgen.exe
/
TAGIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-29
|
14KB
|
393 lines
/*
EPSHeader
File: tagio.c
Author: J. Kercheval
Created: Wed, 07/17/1991 21:48:14
*/
/*
EPSRevision History
J. Kercheval Sun, 08/18/1991 20:49:44 add fill_buffer()
J. Kercheval Tue, 09/03/1991 23:25:13 add buffer_offset to fill_buffer()
J. Kercheval Thu, 09/05/1991 20:13:01 add MergeFile()
J. Kercheval Tue, 09/10/1991 23:55:47 add calls to external_cleanup() when exiting
J. Kercheval Tue, 09/17/1991 19:41:16 add support for case_sensitive flag
J. Kercheval Sat, 03/28/1992 10:26:45 add V6.0 Epsilon format
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include "tagio.h"
#include "log.h"
/* external_cleanup() is used on exit(1) for internal error */
extern void external_cleanup(void);
/*----------------------------------------------------------------------------
*
* FillBuffer() fills the passed buffer parameter with bufsize characters
* (or as many as are available) and places and null character '\0' at the
* end of the buffer. This routine returns TRUE if successful and FALSE if
* eof(infile) is true. Note: if a bufsize parameter is passed and the read
* is successful for bufsize characters, then buffer[bufsize] will be
* overwritten with the null character. Do not pass a bufsize the maximum
* size of the buffer. This null terminated buffering scheme assumes the
* source file has no null character embedded within it.
*
---------------------------------------------------------------------------*/
BOOLEAN FillBuffer(FILE * infile, char *buffer, long int bufsize)
{
long int size;
/* init buffer */
*buffer = '\0';
/* return if end of file */
if (feof(infile))
return FALSE;
/* read the buffer from the file */
size = (long int) fread((void *) buffer, (size_t) sizeof(unsigned char),
(size_t) bufsize, infile);
/* place the end of buffer mark and return success */
buffer[size] = '\0';
return TRUE;
}
/*----------------------------------------------------------------------------
*
* GetLine will read a line not to exceed n-1 chars from f and will return
* true if any characters parsed
*
---------------------------------------------------------------------------*/
BOOLEAN GetLine(FILE * f, char *line, int n)
{
int i; /* looping variable (size of line) */
char c; /* temporary character variable */
/* check for end of file */
if (feof(f))
return FALSE;
for (i = 0, c = '\0'; !feof(f) && c != '\n' && i < n - 1; i++) {
/* get next char */
c = (char) fgetc(f);
/* check for end of line due to n-1th character read */
if (i == n - 2) {
log_message("# getline() -- long line encountered - truncating");
do {
c = (char) fgetc(f);
} while (!feof(f) && c != '\n');
}
/* write the character to the line */
line[i] = c;
}
/* zero length string and end of file return FALSE */
if (feof(f) && i == 1)
return FALSE;
/* write end of line and return */
line[i - 1] = '\0';
return TRUE;
}
/*----------------------------------------------------------------------------
*
* OutputTag() places the tag in the correct format into the output stream
*
---------------------------------------------------------------------------*/
void OutputTag(FILE * outfile, char *line, char *symbol, char *infname,
long int line_number, long int char_number, Flags * flags)
{
/* epsilon style output */
if (flags->oe) {
fprintf(outfile, "%s\t%s\t%ld\t%s\n",
symbol, infname, char_number, line);
return;
}
if (flags->o5) {
fprintf(outfile, "%s;%s;%ld\n", symbol, infname, char_number);
return;
}
/* GNU style output */
if (flags->og) {
fprintf(outfile, "%s\t%s\t/^%s$/\n", symbol, infname, line);
return;
}
/* spaces delimited output */
if (flags->os) {
fprintf(outfile, "%s %s %ld\n", symbol, infname, line_number);
return;
}
/* MicroSoft Error format */
if (flags->om) {
fprintf(outfile, "%s %s(%ld)\n", symbol, infname, line_number);
return;
}
/* not reached */
log_message("# OutputTag -- internal error - continuing");
return;
}
/*----------------------------------------------------------------------------
*
* MergeFile() will take the temporary file and merge it into the potentially
* existing tag file. During the merge, any tags from the input files which
* were processed during this session will be deleted from the tag file to
* prevent innaccurate tagging information.
*
---------------------------------------------------------------------------*/
#define MAX_TAGFILE_LINE_LENGTH 4096
#define MAXPATH 80
void MergeFile(char *input_filename, char *output_filename,
ArgList arglist, Flags * flags)
{
FILE *input_stream; /* file pointer used for input */
FILE *new_tag_stream; /* file pointer used by new tag stream */
FILE *output_stream; /* file pointer used for output */
char line1[MAX_TAGFILE_LINE_LENGTH]; /* line for new tag file */
char line2[MAX_TAGFILE_LINE_LENGTH]; /* line for old tag file */
char tag_filename[MAXPATH]; /* intermediate used to purge old tags */
char delim[] = " \t;("; /* list of valid delimiters for tag format */
char *tmp_filename; /* intermediate file name */
char *token_start; /* pointer to interesting tokens on purge */
int token_length; /* length of token at token_start */
int compare; /* the results of a stricmp() */
BOOLEAN file1ret; /* true while new tag file in not at EOF */
BOOLEAN file2ret; /* true while old tag file in not at EOF */
if (access(output_filename, 00)) {
/* open the new tags file for read */
if ((new_tag_stream = fopen(input_filename, "r")) ==
(FILE *) NULL) {
log_message("# MergeFile() -- error opening input file");
external_cleanup();
exit(1);
}
/* open the tag output file for write */
if ((output_stream = fopen(output_filename, "w")) ==
(FILE *) NULL) {
log_message("# MergeFile() -- error opening tag file");
external_cleanup();
exit(1);
}
/* the old tag output file doesn't exist yet, just copy it over after
* placing the required header if this is the epsilon format. */
if (flags->oe) {
if (flags->sort_tags)
fprintf(output_stream, "\tTAGS\tSORTED\n");
else
fprintf(output_stream, "\tTAGS\tUNSORTED\n");
}
while (GetLine(new_tag_stream, line1, MAX_TAGFILE_LINE_LENGTH)) {
fprintf(output_stream, "%s\n", line1);
}
/* close the files */
fclose(new_tag_stream);
fclose(output_stream);
}
else {
/* create the required temporary file name */
tmp_filename = tempnam(".\\", "tg");
/* open the old tag file */
if ((input_stream = fopen(output_filename, "r")) ==
(FILE *) NULL) {
log_message("# MergeFile() -- error opening output file");
external_cleanup();
exit(1);
}
/* open the temporary file */
if ((output_stream = fopen(tmp_filename, "w")) ==
(FILE *) NULL) {
log_message("# MergeFile() -- error opening temporary file");
external_cleanup();
exit(1);
}
/* if we are using the Epsilon tag format, ignore the header at
* the beginning of the file. */
if (flags->oe) {
if (GetLine(input_stream, line1, MAX_TAGFILE_LINE_LENGTH)) {
fprintf(output_strea